package com.situ.firmadmin.aspect;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
//@Component  new一个对象放到容器中
public class ServiceLogAspect {
    private static final Logger logger= LoggerFactory.getLogger(ServiceLogAspect.class);

    @Around("execution(* com.situ.firmadmin.service.impl.*.*(..))")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("=======开始执行{}.{}==========",joinPoint.getTarget().getClass(),joinPoint.getSignature().getName());

        //记录开始时间
        long begin = System.currentTimeMillis();

        //执行目标对象Service中代码
        Object result = joinPoint.proceed();

        //模拟一个耗时操作
        //Thread.sleep(2000);

        long end = System.currentTimeMillis();
        long takeTime = end - begin;
        if (takeTime > 3000) {
            logger.error("=====执行结束，耗费 {}毫秒=======", takeTime);
        } else if (takeTime > 2000) {
            logger.warn("=====执行结束，耗费 {}毫秒=======", takeTime);
        } else {
            logger.info("=====执行结束，耗费 {}毫秒=======", takeTime);

        }

        return result;
    }
}